package com.sgb.kjwl.viewmodel.home;

import android.content.Context;
import android.text.TextUtils;

import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.EnterpriseAPIManager;
import com.sgb.kjwl.model.entity.Bean;
import com.sgb.kjwl.model.entity.ParentAreaEntity;
import com.sgb.kjwl.model.entity.enterprise.IpPickSelectEntity;
import com.sgb.kjwl.model.entity.enterprise.NeedEntity;
import com.sgb.kjwl.repository.HomeRepertory;
import com.sgb.kjwl.view.ui.activity.manage.WebsiteInfoActivity;
import com.sgb.kjwl.view.ui.fragment.enterprise.NeedFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 作者:张磊
 * 日期:2020/12/21 0021
 * 说明:需求大厅
 */

public class NeedModel extends BaseViewModel {
    private HomeRepertory repertory;
    private NeedFragment mFragment;
    IpPickSelectEntity entity1 = new IpPickSelectEntity("", "不限", "", "", "", "", "");
    public MutableLiveData<List<IpPickSelectEntity>> ldList = new MutableLiveData<>();
    public MutableLiveData<List<IpPickSelectEntity>> ldNewOrOldList = new MutableLiveData<>();

    public String websiteNo;
    public String websiteType;


    public NeedModel(NeedFragment fragment, HomeRepertory repertory) {
        this.mFragment = fragment;
        this.repertory = repertory;
    }

    // 获取商标类型选项
    public void getTrademarkTypeOption() {
        DialogHelper.showProgressDialog(mFragment.getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getNeedAPI().getTrademarkTypeOption().enqueue(new Callback<BaseEntity<List<Bean>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<Bean>>> call, Response<BaseEntity<List<Bean>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() != null) {
                    mFragment.getTrademarkTypeOption(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<Bean>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(mFragment.getActivity(), t.getMessage());
            }
        });
    }

    // 获取所有版权
    public void getAllQualification() {
        DialogHelper.showProgressDialog(mFragment.getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getNeedAPI().getAllQualification().enqueue(new Callback<BaseEntity<List<Bean>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<Bean>>> call, Response<BaseEntity<List<Bean>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() != null) {
                    mFragment.getAllQualification(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<Bean>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(mFragment.getActivity(), t.getMessage());
            }
        });
    }

    // 获取领域分类
    public void getAllCopyright() {
        DialogHelper.showProgressDialog(mFragment.getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getNeedAPI().getAllCopyright().enqueue(new Callback<BaseEntity<List<Bean>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<Bean>>> call, Response<BaseEntity<List<Bean>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() != null) {
                    mFragment.getAllCopyright(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<Bean>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(mFragment.getActivity(), t.getMessage());
            }
        });
    }

    // 行业分类
    public void getAllSserviceCategory() {
        DialogHelper.showProgressDialog(mFragment.getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getNeedAPI().getAllSserviceCategory().enqueue(new Callback<BaseEntity<List<Bean>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<Bean>>> call, Response<BaseEntity<List<Bean>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() != null) {
                    mFragment.getAllSserviceCategory(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<Bean>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(mFragment.getActivity(), t.getMessage());
            }
        });
    }

    // 专利类型
    public void getPatentTypeOption() {
        DialogHelper.showProgressDialog(mFragment.getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getNeedAPI().getPatentTypeOption().enqueue(new Callback<BaseEntity<List<Bean>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<Bean>>> call, Response<BaseEntity<List<Bean>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() != null) {
                    mFragment.getPatentTypeOption(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<Bean>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(mFragment.getActivity(), t.getMessage());
            }
        });
    }

    // 需求大厅列表
    public void getDemandHallList(Map map) {

        if(!TextUtils.isEmpty(websiteType)&&!TextUtils.isEmpty(websiteNo)){
            switch (Integer.parseInt(websiteType)) {
                case WebsiteInfoActivity.WEBSITE_FOR_PERSONAL:
                    map.put("userNo",websiteNo);
                    break;
                case WebsiteInfoActivity.WEBSITE_FOR_TEAM:
                    map.put("teamNo",websiteNo);
                    break;
                case WebsiteInfoActivity.WEBSITE_FOR_COMPANY:
                    map.put("enterpriseNo",websiteNo);
                    break;
            }
        }

        DialogHelper.showProgressDialog(mFragment.getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getNeedAPI().getDemandHallList(map).enqueue(new Callback<BaseEntity<NeedEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<NeedEntity>> call, Response<BaseEntity<NeedEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    mFragment.getDemandHallList(response.body().getData().list);
                } else {
                    MToast.showToast(mFragment.getActivity(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<NeedEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(mFragment.getActivity(), t.getMessage());
            }
        });
    }


    public void getSereicePrice(int type) {
        EnterpriseAPIManager.getInstance().getEnterpriseAPI().getPriceOption(type).enqueue(new Callback<BaseEntity<List<IpPickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<IpPickSelectEntity>>> call, Response<BaseEntity<List<IpPickSelectEntity>>> response) {
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    ArrayList<IpPickSelectEntity> list1 = new ArrayList<>();
                    list1.addAll(response.body().getData());
                    list1.add(0, entity1);
                    mFragment.showMakeupPopWindow(list1);

                } else {
                    MToast.showToast(mFragment.getActivity(), "暂无数据");
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<IpPickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /**
     * 获取服务类别
     */
    public void getServiceType() {
        DialogHelper.showProgressDialog(mFragment.getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        EnterpriseAPIManager.getInstance().getEnterpriseAPI().getServiceCategoryOption().enqueue(new Callback<BaseEntity<List<ParentAreaEntity<List<ParentAreaEntity<List<ParentAreaEntity>>>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<ParentAreaEntity<List<ParentAreaEntity<List<ParentAreaEntity>>>>>>> call, Response<BaseEntity<List<ParentAreaEntity<List<ParentAreaEntity<List<ParentAreaEntity>>>>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    MToast.showToast(mFragment.getActivity(), "暂无数据");
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    mFragment.showServiceWindow(response.body().getData());
                } else {
                    MToast.showToast(mFragment.getActivity(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<ParentAreaEntity<List<ParentAreaEntity<List<ParentAreaEntity>>>>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }


    /**
     * 资产类型
     */
    public void queryPropertyType(Context context) {
        EnterpriseAPIManager.getInstance().getEnterpriseAPI().queryPropertyType().enqueue(new Callback<BaseEntity<List<IpPickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<IpPickSelectEntity>>> call, Response<BaseEntity<List<IpPickSelectEntity>>> response) {

                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                IpPickSelectEntity ipPickSelectEntity = new IpPickSelectEntity("", "不限", "", "", "", "", "");
                List<IpPickSelectEntity> list = new ArrayList<>();
                list.addAll(response.body().getData());
                list.add(0, ipPickSelectEntity);
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    ldList.postValue(list);
                } else {
                    MToast.showToast(context, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<IpPickSelectEntity>>> call, Throwable t) {

            }
        });
    }


    /**
     * 资产新旧
     */
    public void getNewOrOldType(Context context) {
        EnterpriseAPIManager.getInstance().getEnterpriseAPI().getPropertyNewOrOldOption().enqueue(new Callback<BaseEntity<List<IpPickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<IpPickSelectEntity>>> call, Response<BaseEntity<List<IpPickSelectEntity>>> response) {
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                IpPickSelectEntity ipPickSelectEntity = new IpPickSelectEntity("", "不限", "", "", "", "", "");
                List<IpPickSelectEntity> list = new ArrayList<>();
                list.addAll(response.body().getData());
                list.add(0, ipPickSelectEntity);
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    ldNewOrOldList.postValue(list);
                } else {
                    MToast.showToast(context, "暂无数据");
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<IpPickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}
